﻿#region File Description
//  XAEnima 
//  License: GNU/GPL
//  Developer: Federico Leonardo Motta
//  File description: StateManager is the kernell of XAEnima. 
//  It loads the screen and permits the dialog between components.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using XAEnima.Core.GameManager;
using XAEnima.Core.Screens;
using XAEnima.Core.Screens.Menu;
using XAEnima.Core.Screens.Menu.MenuOptions;
using XAEnima.Core.Screens.Test;
using XAEnima.Core.Screens.Domotica;


namespace XAEnima.Core
{
    public static class StateManager
    {
        public static short[] freq = new short[4];
        public static GraphicsDeviceManager graphic;
        private static void Cleanup(this Game game)
        {
            for (int i = 0; i < game.Components.Count; i++)
            {
                (game.Components[i] as GameComponent).Dispose();

                i--;
            }
        }

        private static void GameQuit(this Game game) {
            game.Exit();
        }

        public static void StartIntro(this Game game)
        {
            game.Cleanup();
            game.Components.Add(new InputManager(game));
            game.Components.Add(new ComunicationManager(game));
            var intro = new Intro(game);
            intro.OnFinish += () => game.StartMenu();
            game.Components.Add(intro);

        }

        public static void StartGame(this Game game)
        {
            game.Cleanup();
            game.Components.Add(new InputManager(game));
            game.Components.Add(new ComunicationManager(game));
            var domotica = new Domotica(game);
            domotica.OnBack += () => game.StartMenu();
            domotica.OnEnvironmentSelect += () => game.StartEnv();
            game.Components.Add(domotica);

        }

        public static void StartEnv(this Game game)
        {
            game.Cleanup();
            game.Components.Add(new InputManager(game));
            game.Components.Add(new ComunicationManager(game));
            var ambiente = new Ambiente(game);
            ambiente.OnBack += () => game.StartGame();
            game.Components.Add(ambiente);
        }

        public static void StartMenu(this Game game)
        {
            game.Cleanup();
            game.Components.Add(new InputManager(game));
            game.Components.Add(new ComunicationManager(game));
            var Menu = new MainMenu(game);
            Menu.OnTest += () => game.TestsMenu();
            Menu.OnCredits += () => game.Credits();
            Menu.OnOptions += () => game.Options();
            Menu.OnPlay += () => game.StartGame();
            Menu.OnQuit += () => game.GameQuit();
            game.Components.Add(Menu);
        }

        public static void Options(this Game game) {
            game.Cleanup();
            game.Components.Add(new InputManager(game));
            var options = new Options(game);
            options.OnBack += () => game.StartMenu();
            options.OnVideo += () => game.VideoOptions();
            options.OnVarie += () => game.Varie();
            game.Components.Add(options);
        }

        public static void VideoOptions(this Game game) {
            game.Cleanup();
            game.Components.Add(new InputManager(game));
            var video = new VideoOptions(game);
            video.OnBack += () => game.Options();
            game.Components.Add(video);
        }

        public static void Varie(this Game game) {
            game.Cleanup();
            game.Components.Add(new InputManager(game));
            var varie = new Varie(game);
            varie.OnBack += () => game.Options();
            game.Components.Add(varie);
        }

        public static void Credits(this Game game) {
            game.Cleanup();
            game.Components.Add(new InputManager(game));
            var credits = new Credits(game);
            credits.OnQuit += () => game.StartMenu();
            game.Components.Add(credits);
        }


        public static void TestsMenu(this Game game)
        {
            game.Cleanup();
            game.Components.Add(new InputManager(game));
            var test_menu = new TestMenu(game);
            test_menu.OnScreening += () => game.Screening();
            test_menu.OnTesting += () => game.Test();
            test_menu.OnTraining += () => game.Training();
            test_menu.OnBack += () => game.StartMenu();
            //tests.OnQuit += () => game.StartMenu();

            game.Components.Add(test_menu);
        }

        public static void Screening(this Game game)
        {
            game.Cleanup();
            game.Components.Add(new InputManager(game));
            game.Components.Add(new ComunicationManager(game));
            var screening = new Screening(game);
            screening.OnBack += () => game.TestsMenu();
            //tests.OnQuit += () => game.StartMenu();

            game.Components.Add(screening);
        }

        public static void Training(this Game game)
        {
            game.Cleanup();
            game.Components.Add(new InputManager(game));
            game.Components.Add(new ComunicationManager(game));
            var train = new Training(game);
            train.OnBack += () => game.TestsMenu();
            //tests.OnQuit += () => game.StartMenu();

            game.Components.Add(train);
        }

        public static void Test(this Game game)
        {
            game.Cleanup();
            game.Components.Add(new InputManager(game));
            game.Components.Add(new ComunicationManager(game));
            var test = new Test(game);
            //var test = new Testtest(game);
            test.OnBack += () => game.TestsMenu();
            //tests.OnQuit += () => game.StartMenu();

            game.Components.Add(test);
        }


        public static void ComponentDispose(this Game game, GameComponent component) {
            game.Components.Remove(component);
            component.Dispose();

        }

        public static void GetGraphic(this Game game,GraphicsDeviceManager graphics)
        {
            graphic = graphics;

        }
    }
}
